﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Ldy.Core
{
    /// <summary>
    /// lil 访问功能类 {21EB22A9-1A47-4EDB-BD53-FE030755C7D8}
    /// </summary>
    public class Http
    {
        public static string DeleteAsync(string url, string data = null)
        {
            Log.Info("【DeleteAsync start。。。。。。。。】");
            Log.Info("【Url】：" + url);
            Log.Info("【In Parameter】:" + data);
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    Task<HttpResponseMessage> httpResponse = client.DeleteAsync(url);
                    if (httpResponse == null) return null;
                    HttpResponseMessage response = httpResponse.Result;
                    string result = response.Content.ReadAsStringAsync().Result;
                    Log.Info("【Out Parameter】:" + result);
                    Log.Info("【DeleteAsync end。。。。。。。。】");
                    return result;
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message);
                    return null;
                }
            }
        }

        public static T DeleteAsync<T>(string url, string data = null)
        {
            string result = DeleteAsync(url, data);
            if (result == null) return default;
            return JsonConvert.DeserializeObject<T>(result);
        }

        public static string GetAsync(string url)
        {
            Log.Info("【GetAsync start。。。。。。。。】");
            Log.Info("【Url】：" + url);
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    Task<HttpResponseMessage> httpResponse = client.GetAsync(url);
                    if (httpResponse == null) return null;
                    HttpResponseMessage response = httpResponse.Result;
                    string result = response.Content.ReadAsStringAsync().Result;
                    Log.Info("【Out Parameter】:" + result);
                    Log.Info("【GetAsync end。。。。。。。。】");
                    return result;
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message);
                    return null;
                }
            }
        }

        public static T GetAsync<T>(string url)
        {
            string result = GetAsync(url);
            if (result == null) return default;
            return JsonConvert.DeserializeObject<T>(result);
        }

        public static string PostAsync(string url, string data = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string> headers = null)
        {
            Log.Info("【PostAsync start。。。。。。。。】");
            Log.Info("【Url】：" + url);
            Log.Info("【In Parameter】:" + data);
            using (HttpClient client = new HttpClient())
            {
                if (headers != null)
                    headers.ToList().ForEach(d => client.DefaultRequestHeaders.Add(d.Key, d.Value));
                using (StringContent content = new StringContent(data, Encoding.UTF8))
                {
                    if (contentType != null)
                        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
                    try
                    {
                        Task<HttpResponseMessage> httpResponse = client.PostAsync(url, content);
                        if (httpResponse == null) return null;
                        HttpResponseMessage response = httpResponse.Result;
                        string result = response.Content.ReadAsStringAsync().Result;
                        Log.Info("【Out Parameter】:" + result);
                        Log.Info("【PostAsync end。。。。。。。。】");
                        return result;
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.Message);
                        return null;
                    }
                }
            }
        }

        public static string PostAsync(string url, object data = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string> headers = null)
        {
            if (data == null) return default;
            string d = JsonConvert.SerializeObject(data);
            return PostAsync(url, d, contentType, timeOut, headers);
        }

        public static T PostAsync<T>(string url, string data = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string> headers = null)
        {
            string result = PostAsync(url, data, contentType, timeOut, headers);
            if (result == null) return default;
            return JsonConvert.DeserializeObject<T>(result);
        }

        public static T PostAsync<T>(string url, object data = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string> headers = null)
        {
            if (data == null) return default;
            string d = JsonConvert.SerializeObject(data);
            return PostAsync<T>(url, d, contentType, timeOut, headers);
        }

        public static string PutAsync(string url, string data = null, string contentType = null, int timeOut = 60, Dictionary<string, string> headers = null)
        {
            Log.Info("【PutAsync start。。。。。。。。】");
            Log.Info("【Url】：" + url);
            Log.Info("【In Parameter】:" + data);
            using (HttpClient client = new HttpClient())
            {
                if (headers != null)
                    headers.ToList().ForEach(d => client.DefaultRequestHeaders.Add(d.Key, d.Value));
                using (StringContent content = new StringContent(data, Encoding.UTF8))
                {
                    if (contentType != null)
                        content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
                    try
                    {
                        Task<HttpResponseMessage> httpResponse = client.PutAsync(url, content);
                        if (httpResponse == null) return null;
                        HttpResponseMessage response = httpResponse.Result;
                        string result = response.Content.ReadAsStringAsync().Result;
                        Log.Info("【Out Parameter】:" + result);
                        Log.Info("【PutAsync end。。。。。。。。】");
                        return result;
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.Message);
                        return null;
                    }
                }
            }
        }

        public static string PutAsync(string url, object data = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string> headers = null)
        {
            if (data == null) return default;
            string d = JsonConvert.SerializeObject(data);
            return PutAsync(url, d, contentType, timeOut, headers);
        }

        public static T PutAsync<T>(string url, string data = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string> headers = null)
        {
            string result = PutAsync(url, data, contentType, timeOut, headers);
            if (result == null) return default;
            return JsonConvert.DeserializeObject<T>(result);
        }

        public static T PutAsync<T>(string url, object data = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string> headers = null)
        {
            if (data == null) return default;
            string d = JsonConvert.SerializeObject(data);
            return PutAsync<T>(url, d, contentType, timeOut, headers);
        }

    }
}
